//
//  InfoTableViewController.m
//  JiongTu
//
//  Created by 杨璟 on 16/3/31.
//  Copyright © 2016年 YSKJ. All rights reserved.
//

#define NOTIF_LOC [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"notification.archive"]

#import "InfoTableViewController.h"

#import "NotificationViewCell.h"

#import "MyOrderViewController.h"
#import "JTBaseNaviController.h"
#import "PassengerInfoViewController.h"

#import "JTPushNotification.h"
#import "JTPushTool.h"
#import "JTPassengerTrip.h"

#import <SVProgressHUD.h>

@interface InfoTableViewController ()

@property (nonatomic, strong) NSMutableArray *notifications;

@property (nonatomic, strong) NSMutableArray *trips;

@end

@implementation InfoTableViewController

- (void)setPushInfo:(NSDictionary *)pushInfo {
    _pushInfo = pushInfo;
}

- (void)setNotifications:(NSMutableArray *)notifications {
    _notifications = notifications;
    
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSMutableArray *notifs = [JTPushTool currentNotifications];
    if (notifs.count) {
        self.notifications = notifs;
    }
    
    //如果导航控制器含有其他控制器，则是被push出来
    NSArray *viewcontrollers=self.navigationController.viewControllers;
    if (viewcontrollers.count>1) {
        if ([viewcontrollers objectAtIndex:viewcontrollers.count-1] == self) {
            self.navigationItem.rightBarButtonItem = nil;
        }
        //反之没有其他控制器则是被present出来
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.notifications.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NotificationViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"infoCell" forIndexPath:indexPath];
    cell.oneNotification = self.notifications[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    JTPushNotification *oneNotif = self.notifications[indexPath.row];
    UIStoryboard *sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    if ([oneNotif.identity isEqualToString:@"乘客"]) {
//        AVQuery * query = [AVQuery queryWithClassName:@"PassengerTrip"];
//        [query whereKey:@"objectId" equalTo:self.pushOrder.passengerTripId];
//        [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
//            [SVProgressHUD dismiss];
//            self.tripArray = [NSMutableArray arrayWithArray:@[[JTPassengerTrip tripWithAVObject:object]]];
//        }];
//        for (JTDriverTrip *theDriverTrip in self.tripArray) {
//            for (JTPassengerTrip *theDriversPass in theDriverTrip.passenger) {
//                if ([theDriversPass.objectId isEqualToString:self.pushOrder.passengerTripId]) {
//                    NSInteger position = [self.tripArray indexOfObject:theDriverTrip];
//                    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:position inSection:0];
//                    [self.orderTable.delegate tableView:self.orderTable didSelectRowAtIndexPath:indexPath];
//                }
//            }
//        }
        [SVProgressHUD showWithStatus:@"正在查询乘客信息"];
        PassengerInfoViewController *checkVC = [sb instantiateViewControllerWithIdentifier:@"passInfo"];
        NSError *error = [NSError errorWithDomain:@"zh_CN" code:0 userInfo:nil];
        JTPassengerTrip *passTrip = [JTPassengerTrip tripWithObjectId:oneNotif.passengerTripId error:error];
        if (!passTrip) {
            [SVProgressHUD showErrorWithStatus:error.localizedDescription];
        } else {
            checkVC.onePassenger = passTrip;
            [self presentViewController:checkVC animated:YES completion:^{
                [SVProgressHUD dismiss];
            }];
        }
        
    } else if ([oneNotif.identity isEqualToString:@"司机"]) {
//        AVQuery * query = [AVQuery queryWithClassName:@"DriverTrip"];
//        [query whereKey:@"objectId" equalTo:self.pushOrder.driverTripId];
//        [query getFirstObjectInBackgroundWithBlock:^(AVObject *object, NSError *error) {
//            [SVProgressHUD dismiss];
//            self.tripArray = [NSMutableArray arrayWithArray:@[[JTDriverTrip tripWithAVObject:object]]];
//        }];
        
        MyOrderViewController *vc = [sb instantiateViewControllerWithIdentifier:@"myorder"];
        vc.pushOrder = oneNotif;
        [self.navigationController pushViewController:vc animated:YES];

    }
    
#warning open clear 打开这一行会在点击时清除消息
//    if (self.notifications.count) {
//        [self.notifications removeObjectAtIndex:indexPath.row];
//        [JTPushTool editNotifications:self.notifications];
//        [tableView reloadData];
//    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    return YES;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
//    [tableView setEditing:YES animated:YES];
    return UITableViewCellEditingStyleDelete;
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.notifications.count) {
        [self.notifications removeObjectAtIndex:indexPath.row];
        [JTPushTool editNotifications:self.notifications];
    }
    [tableView reloadData];
}

- (NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath {
    return @"删除";
}

#pragma mark - dismiss操作

- (IBAction)dismissClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

@end
